package compareResult.datasetCompare;

import com.supermap.data.*;

/**
 * Created by hanyz on 2016/11/30.
 */
public class DatasetComparer {
	public double tolerance = 0.00001d;
	public String compareError;

	public boolean compare(Dataset expDataset, Dataset actualDataset) throws Exception {
		//数据集如果没有通过点击等方式被动打开过，有些属性获取不到，如果getEncodeType
		expDataset.open();
		actualDataset.open();

		if (expDataset instanceof DatasetVector) {
			return new DatasetVectorComparer().compare((DatasetVector) expDataset, (DatasetVector) actualDataset);
		}
		if (expDataset instanceof DatasetGrid) {
			return new DatasetGridComparer().compare((DatasetGrid) expDataset, (DatasetGrid) actualDataset);
		}
		if (expDataset instanceof DatasetImage) {
			return new DatasetImageComparer().compare((DatasetImage) expDataset, (DatasetImage) actualDataset);
		}
		throw new Exception("Dataset Type Comparer not suppored");
	}

	public boolean compareCommon(Dataset expDataset, Dataset actualDataset) throws Exception {
		compareError = "Dataset " + expDataset.getName() + "," + actualDataset.getName() + " compareError:\n";
		if (expDataset == null || actualDataset == null) {
			compareError += "expDataset:" + expDataset + "\nactualDataset:" + actualDataset;
			throw new Exception(compareError);
		}
		//数据集类型
		if (!expDataset.getType().equals(actualDataset.getType())) {
			compareError += "expDataset type:" + expDataset.getType().toString() + "\nactualDataset type:" + actualDataset.getType().toString();
			throw new Exception(compareError);
		}
		//编码
		if (!expDataset.getEncodeType().equals(actualDataset.getEncodeType())) {
			compareError += "expDataset EncodeType:" + expDataset.getEncodeType().toString() + "\nactualDataset EncodeType:" + actualDataset.getEncodeType().toString();
			throw new Exception(compareError);
		}
		//投影
		try {
			CompareUtilities.comparePrjCoordSys(expDataset.getPrjCoordSys(), actualDataset.getPrjCoordSys());
		} catch (Exception e) {
			compareError += e.getMessage();
			throw new Exception(compareError);
		}
		//数据集范围
		if (expDataset.getBounds() == null || actualDataset.getBounds() == null) {
			compareError += "expDataset bounds:" + expDataset.getBounds() + "actualDataset bounds:" + actualDataset.getBounds();
			throw new Exception(compareError);
		}
		double maxEqualZeroPrecision = Environment.getMaxEqualZeroPrecision();
		double minEqualZeroPrecision = Environment.getMinEqualZeroPrecision();
		try {
			//调用bounds自带的比较方法需要设置环境精度。使用过后再置回原先的精度，以免影响其他功能。
			Environment.setMaxEqualZeroPrecision(0.00001);
			Environment.setMinEqualZeroPrecision(-0.00001);
			if (!expDataset.getBounds().equals(actualDataset.getBounds())) {
				compareError += "expDataset bounds TLBR:" + expDataset.getBounds().getTop() + " " + expDataset.getBounds().getLeft()
						+ " " + expDataset.getBounds().getBottom() + " " + expDataset.getBounds().getRight() + "\n"
						+ "actualDataset bounds TLBR:" + actualDataset.getBounds().getTop() + " " + actualDataset.getBounds().getLeft()
						+ " " + actualDataset.getBounds().getBottom() + " " + actualDataset.getBounds().getRight();
				throw new Exception(compareError);
			}
		} finally {
			Environment.setMaxEqualZeroPrecision(maxEqualZeroPrecision);
			Environment.setMinEqualZeroPrecision(minEqualZeroPrecision);
		}
		return true;
	}

}
